<?php
    $conn = mysql_connect("192.168.10.12", "root", "qifa2017!@#");
    
    if (!$conn) {
        echo "Unable to connect to DB: " . mysql_error();
        exit;
    }
    
    if (!mysql_select_db("qifa2")) {
        echo "Unable to select mydbname: " . mysql_error();
        exit;
    }
    mysql_query("SET NAMES utf8");
    $sql = "SELECT `cat_id`, `parent_id`, `cat_name`  FROM `sdb_b2c_goods_cat`"; //query for category

    $result = mysql_query($sql);

    if (!$result) {
        echo "Could not successfully run query ($sql) from DB: " . mysql_error();
        exit;
    }
    
    if (mysql_num_rows($result) == 0) {
        echo "No rows found, nothing to print so am exiting";
        exit;
    }
date_default_timezone_set('Europe/Moscow');
//$text - it's a file content
$text = '<?xml version="1.0" encoding="UTF-8"?>'."\r";
$text .= '<yml_catalog date="'.date('Y-m-d H:i').'">'."\r";
$text .= '<shop>
<name>QIFA</name>
<company>QIFA</company>
<url>https://www.qifa.ru</url>
<platform></platform>
<version></version>
<agency></agency>
<email></email>
<currencies>
<currency id="RUR" rate="1"/>
</currencies>
<categories>'."\r";

//categories	
    while ($row = mysql_fetch_assoc($result)) {
		if($row["parent_id"] != 0){
			$parentId = 'parentId="'.$row["parent_id"].'"';
		}
		else{
			$parentId = '';
		}
		$text .= '<category id="'.$row["cat_id"].'" '.$parentId.'>'.$row["cat_name"].'</category>'."\r";	
    }

$text .= '</categories>'."\r";

$text .= '<offers>'."\r";

//params
$paramsSql = "SELECT `props_value_id`,`name` FROM `sdb_b2c_goods_type_props_value`"; //query for items

$paramsRes = mysql_query($paramsSql);

$params = array();
while ($paramsRow = mysql_fetch_assoc($paramsRes)) {
	$params[$paramsRow['props_value_id']] = $paramsRow['name'];
}

//items

$itemsSql = "SELECT g.goods_id, g.bn, g.name, g.cat_id, g.s_price, g.p_1, g.p_2, g.p_3, g.p_4, g.p_5, g.p_6, g.p_7, g.p_8, g.p_9, g.p_10, g.p_11, g.p_12, g.p_13, g.p_14, g.p_15, g.p_16, i.url, b.brand_name, p.product_id
FROM sdb_b2c_goods AS g 
INNER JOIN sdb_b2c_brand AS b 
INNER JOIN sdb_image_image AS i 
INNER JOIN sdb_b2c_products AS p 
WHERE g.marketable = true
AND b.brand_id = g.brand_id
AND i.image_id = g.image_default_id 
AND p.goods_id = g.goods_id"; 

$itemsRes = mysql_query($itemsSql);

while ($itemsRow = mysql_fetch_assoc($itemsRes)) {
$text .= '<offer id="'.$itemsRow["goods_id"].'" type="vendor.model" available="true" selling_type="w">
<url>https://www.qifa.ru/product-'.$itemsRow["product_id"].'.html</url>
<price>'.floor($itemsRow["s_price"]).'</price>
<currencyId>RUR</currencyId>
<categoryId>'.$itemsRow["cat_id"].'</categoryId>
<picture>https://www.qifa.ru/'.$itemsRow["url"].'</picture>
<market_category>Одежда, обувь и аксессуары/Обувь</market_category>
<model>'.htmlentities($itemsRow["name"],ENT_QUOTES, "UTF-8").' '.htmlentities($itemsRow["bn"],ENT_QUOTES, "UTF-8").'</model>
<vendorCode>'.htmlentities($itemsRow["bn"],ENT_QUOTES, "UTF-8").'</vendorCode>
<vendor>'.htmlentities($itemsRow["brand_name"],ENT_QUOTES, "UTF-8").'</vendor>'."\r";
if(isset($itemsRow["p_1"]) && !empty($itemsRow["p_1"])){$text .= '<param name="Сезон">'.htmlentities($params[$itemsRow["p_1"]],ENT_QUOTES, "UTF-8").'</param>'."\r";}
if(isset($itemsRow["p_2"]) && !empty($itemsRow["p_2"])){$text .= '<param name="Вид обуви">'.htmlentities($params[$itemsRow["p_2"]],ENT_QUOTES, "UTF-8").'</param>'."\r";}
if(isset($itemsRow["p_3"]) && !empty($itemsRow["p_3"])){$text .= '<param name="Возростная группа">'.htmlentities($params[$itemsRow["p_3"]],ENT_QUOTES, "UTF-8").'</param>'."\r";}
if(isset($itemsRow["p_4"]) && !empty($itemsRow["p_4"])){$text .= '<param name="Детский пол">'.htmlentities($params[$itemsRow["p_4"]],ENT_QUOTES, "UTF-8").'</param>'."\r";}
if(isset($itemsRow["p_5"]) && !empty($itemsRow["p_5"])){$text .= '<param name="Количество пар в упаковке">'.htmlentities($params[$itemsRow["p_5"]],ENT_QUOTES, "UTF-8").'</param>'."\r";}
if(isset($itemsRow["p_6"]) && !empty($itemsRow["p_6"])){$text .= '<param name="Цвет">'.htmlentities($params[$itemsRow["p_6"]],ENT_QUOTES, "UTF-8").'</param>'."\r";}
if(isset($itemsRow["p_7"]) && !empty($itemsRow["p_7"])){$text .= '<param name="Материал подклада">'.htmlentities($params[$itemsRow["p_7"]],ENT_QUOTES, "UTF-8").'</param>'."\r";}
if(isset($itemsRow["p_8"]) && !empty($itemsRow["p_8"])){$text .= '<param name="Индивидуальная упаковка">'.htmlentities($params[$itemsRow["p_8"]],ENT_QUOTES, "UTF-8").'</param>'."\r";}
if(isset($itemsRow["p_9"]) && !empty($itemsRow["p_9"])){$text .= '<param name="Категория">'.htmlentities($params[$itemsRow["p_9"]],ENT_QUOTES, "UTF-8").'</param>'."\r";}
if(isset($itemsRow["p_10"]) && !empty($itemsRow["p_10"])){$text .= '<param name="Размерный ряд">'.htmlentities($params[$itemsRow["p_10"]],ENT_QUOTES, "UTF-8").'</param>'."\r";}
if(isset($itemsRow["p_11"]) && !empty($itemsRow["p_11"])){$text .= '<param name="Материал верха">'.htmlentities($params[$itemsRow["p_11"]],ENT_QUOTES, "UTF-8").'</param>'."\r";}
if(isset($itemsRow["p_12"]) && !empty($itemsRow["p_12"])){$text .= '<param name="Материал подошвы">'.htmlentities($params[$itemsRow["p_12"]],ENT_QUOTES, "UTF-8").'</param>'."\r";}
if(isset($itemsRow["p_13"]) && !empty($itemsRow["p_13"])){$text .= '<param name="Материал стельки">'.htmlentities($params[$itemsRow["p_13"]],ENT_QUOTES, "UTF-8").'</param>'."\r";}
if(isset($itemsRow["p_14"]) && !empty($itemsRow["p_14"])){$text .= '<param name="Широкая нога">'.htmlentities($params[$itemsRow["p_14"]],ENT_QUOTES, "UTF-8").'</param>'."\r";}
if(isset($itemsRow["p_15"]) && !empty($itemsRow["p_15"])){$text .= '<param name="Другие параметры">'.htmlentities($params[$itemsRow["p_15"]],ENT_QUOTES, "UTF-8").'</param>'."\r";}
if(isset($itemsRow["p_16"]) && !empty($itemsRow["p_16"])){$text .= '<param name="Высота каблука">'.htmlentities($params[$itemsRow["p_16"]],ENT_QUOTES, "UTF-8").'</param>'."\r";}
$text .= '<param name="picture_url">https://www.qifa.ru/'.$itemsRow["url"].'</param>'."\r";
$text .= '</offer>'."\r";	
}	
$text .= '</offers>
</shop>
</yml_catalog>';		
	
	
 
//open file market.yml; create if not exists
//$fp = fopen("market.yml", "w");
 $fp = fopen("/data/site.com/market.yml", "w");
 
// write text to file
fwrite($fp, $text);
 
// file close 
fclose($fp);

?>